package com.leejxx.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.leejxx.health.dao.PermissionDao;
import com.leejxx.health.entity.PageResult;
import com.leejxx.health.entity.QueryPageBean;
import com.leejxx.health.exception.BusinessRuntimeException;
import com.leejxx.health.pojo.Permission;
import com.leejxx.health.service.PermissionService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author LeeJx
 * @ClassName PermissionServiceImpl
 * @Description TODO
 * @date 2021/1/11 9:00
 * @Version 1.0
 */
@DubboService
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Permission> page = permissionDao.findByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void add(Permission permission) {
        if (null == permission.getName() || null == permission.getKeyword()) {
            throw new BusinessRuntimeException("权限名称或关键字不能为空，请重新输入");
        }
        permissionDao.add(permission);
    }

    @Override
    public Permission findById(Integer permissionId) {
        return permissionDao.findById(permissionId);
    }

    @Override
    public void edit(Permission permission) {
        if (null == permission.getName() || null == permission.getKeyword()) {
            throw new BusinessRuntimeException("权限名称或关键字不能为空，请重新输入");
        }
        permissionDao.edit(permission);
    }

    @Override
    public void delete(Integer permissionId) {
        permissionDao.delete(permissionId);
    }
}
